<template>
  <div class="login">
    <div class="login_box">
      <img src="../assets/img/logo.png" alt="" />
      <h1>登录界面</h1>
      <el-form :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input
            clearable
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            clearable
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-s-goods"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button
            class="login_btn"
            type="primary"
            @click="login('loginForm')"
            >登录</el-button
          >
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { postUserLogin } from "../util/axios";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapActions(["getUserInfoAction"]),
    login(loginForm) {
      this.$refs[loginForm].validate((valid) => {
        if (valid) {
          postUserLogin(this.loginForm).then((res) => {
            if (res.data.code == 200) {
              this.getUserInfoAction(res.data.list);
              this.$message.success(res.data.msg);
              this.$router.push("/index");
              //登录成功之后，把存储信息存到本地存储中
              // sessionStorage.setItem('userInfo',JSON.stringify(res.data.list))
            } else {
              this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
        }
      });
    },
  },
};
</script>

<style lang='less' scoped>
.login {
  width: 100vw;
  height: 100vh;
  background-image: url("../assets/img/bg1.jpg");
  background-size: cover;
  .login_box {
    position: relative;
    width: 450px;
    height: 350px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -175px 0 0 -225px;
    border-radius: 10px;
    padding: 0 35px;
    background-color: rgba(255, 255, 255, 0.2);
    box-sizing: border-box;
    img {
      position: absolute;
      width: 60px;
      height: 60px;
      left: 190px;
      top: -45px;
      border-radius: 10px;
    }
    h1 {
      text-align: center;
      margin: 30px 0;
      font-size: 28px;
      font-weight: 600;
      color: #333;
      letter-spacing: 0.1em;
    }
    .btn {
      margin-top: 50px;
    }
    .login_btn {
      margin-left: 100px;
    }
  }
}
</style>
